package com.lee.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lee.aspect.Log;
import com.lee.base.BaseController;
import com.lee.base.Result;
import com.lee.constant.BusinessType;
import com.lee.page.PageResult;
import com.lee.page.TableSupport;
import com.lee.system.api.IDictTypeService;
import com.lee.system.entity.DictType;
import com.lee.utils.ShiroUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("字典类型管理")
@Controller
@RequestMapping("/system/dict")
public class DictTypeController extends BaseController {

	private String prefix = "system/dict/type";
	@Reference
	private IDictTypeService dictTypeService;

	@RequiresPermissions("system:dict:view")
	@GetMapping()
	public String dictType() {
		return prefix + "/type";
	}

	@ApiOperation("字典类型列表查询")
	@RequiresPermissions("system:dict:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(DictType dictType) {
		PageResult pageResult = dictTypeService.selectDictTypeList(dictType,TableSupport.getPageDomain());
		return Result.ok().page(pageResult.getRows());
	}

	/**
	 * 新增字典类型
	 * 
	 * @return
	 */
	@Log(title = "字典类型", action = BusinessType.INSERT)
	@RequiresPermissions("system:dict:add")
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 修改字典类型
	 * 
	 * @param dictId
	 * @param map
	 * @return
	 */
	@Log(title = "字典类型", action = BusinessType.UPDATE)
	@RequiresPermissions("system:dict:edit")
	@GetMapping("/edit/{dictId}")
	public String edit(@PathVariable("dictId") Long dictId, Map<String, Object> map) {
		DictType dictType = dictTypeService.selectDictTypeById(dictId);
		map.put("dict", dictType);
		return prefix + "/edit";
	}

	/**
	 * 保存字典类型
	 * 
	 * @param dictType
	 * @return
	 */
	@Log(title = "字典类型", action = BusinessType.SAVE)
	@ApiOperation("字典类型保存")
	@PostMapping("/save")
	@RequiresPermissions("system:dict:save")
	@ResponseBody
	public Result save(DictType dictType) {
		dictType.setUpdateBy(ShiroUtils.getLoginName());
		if (dictTypeService.saveDictType(dictType) > 0) {
			return Result.ok();
		}
		return Result.error();
	}

	/**
	 * 删除字典类型
	 * 
	 * @param ids
	 * @return
	 */
	@Log(title = "字典类型", action = BusinessType.DELETE)
	@ApiOperation("删除字典类型")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		try {
			dictTypeService.deleteDictTypeByIds(ids);
			return Result.ok();
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 校验字典类型唯一
	 * 
	 * @param dictType
	 * @return
	 */
	@ApiOperation("字典类型唯一")
	@PostMapping("/checkDictTypeUnique")
	@ResponseBody
	public Result checkDictTypeUnique(DictType dictType) {
		if (dictTypeService.checkDictTypeUnique(dictType)) {
			return Result.ok();
		}
		return Result.error();
	}
	/**
	 * 查询字典类型详情数据
	 * @param dictId
	 * @param map
	 * @return
	 */
	@RequiresPermissions("system:dict:list")
	@GetMapping("/detail/{dictId}")
	public String detail(@PathVariable("dictId")Long dictId,Map<String, Object> map){
		DictType dictType = dictTypeService.selectDictTypeById(dictId);
		List<DictType> dictTypes = dictTypeService.selectDictTypeAll();
		map.put("dict", dictType);
		map.put("dictList",dictTypes);
		return "system/dict/data/data";
	}
}
